import java.awt.Graphics;
import java.awt.Image;
import java.io.Serializable;
import java.util.ArrayList;

import javax.swing.ImageIcon;

public class Map implements Serializable
{

    private static final long serialVersionUID = 146912571761219038L;
    private ImageIcon mapImage;
    private ArrayList<MapObject> objects;
    private double zoomLevel = 1;
    private int imageStartPosX;
    private int imageStartPosY;
    private int imageEndPosX;
    private int imageEndPosY;
    private MapPanel containingPanel;
	public String title;

    public Map()
    {
	this.mapImage = new ImageIcon();
    }

    public Map(Image image, MapPanel containingPanel)
    {

	this.mapImage = new ImageIcon(image);

	this.containingPanel = containingPanel;

	this.imageStartPosX = this.containingPanel.getWidth() / 2 - (image.getWidth(null) / 2);
	this.imageStartPosY = this.containingPanel.getHeight() / 2 - (image.getHeight(null) / 2);
	this.imageEndPosX = this.imageStartPosX + this.mapImage.getIconWidth();
	this.imageEndPosY = this.imageStartPosY + this.mapImage.getIconHeight();

	this.objects = new ArrayList<MapObject>();
	
	this.title = ""+this.containingPanel.getMapList().size();
    }

    public void paint(Graphics g)
    {
	this.imageStartPosX = this.containingPanel.getWidth() / 2 - (int) ((this.mapImage.getIconWidth() / 2) * this.zoomLevel);
	this.imageStartPosY = this.containingPanel.getHeight() / 2 - (int) ((this.mapImage.getIconHeight() / 2) * this.zoomLevel);
	this.imageEndPosX = this.imageStartPosX + (int) (this.mapImage.getIconWidth() * this.zoomLevel);
	this.imageEndPosY = this.imageStartPosY + (int) (this.mapImage.getIconHeight() * this.zoomLevel);

	g.drawImage(this.mapImage.getImage(), this.imageStartPosX, this.imageStartPosY, this.imageEndPosX - this.imageStartPosX, this.imageEndPosY - this.imageStartPosY, null);

	for (MapObject object : this.objects)
	{
	    object.scaleCoords();
	    object.paint(g);

	}
    }

    public ImageIcon getMapImage()
    {
	return this.mapImage;
    }

    public ArrayList<MapObject> getMapObjects()
    {
	return this.objects;
    }

    public void addMapObject(MapObject object)
    {
	this.objects.add(object);
    }

    public void removeMapObject(MapObject object)
    {
	this.objects.remove(object);
    }

    public void increaseZoom()
    {
	this.zoomLevel += .1;
    }

    public void decreaseZoom()
    {
	if (this.zoomLevel >= 0)
	{
	    this.zoomLevel -= .1;
	}
    }

    public double getZoomLevel()
    {
	return this.zoomLevel;
    }

    public int getStartXCoord()
    {
	return this.imageStartPosX;
    }

    public int getStartYCoord()
    {
	return this.imageStartPosY;
    }

    public int getEndXCoord()
    {
	return this.imageEndPosX;
    }

    public int getEndYCoord()
    {
	return this.imageEndPosY;
    }

    public void setMapImage(ImageIcon image)
    {
	this.mapImage = image;
    }

    /*
     * public void save(Properties properties, String name){ //save image int i = 0; for(MapObject object : objects){ object.save(properties, name+"_obj"+i); i++; } properties.setProperty("zoomLevel", this.zoomLevel+""); // properties.setProperty("imageStartPosX", imageStartPosX); // properties.setProperty("imageStartPosY", imageStartPosY); // properties.setProperty("imageEndPosX", imageEndPosX); // properties.setProperty("imageEndPosY", imageEndPosY); }
     */

}
